package com.noahplat.oauth.controller;

import com.alibaba.fastjson.JSONObject;
import com.noahplat.common.utils.Result;
import com.noahplat.oauth.service.UserLoginService;
import com.noahplat.oauth.utils.AuthToken;
import com.noahplat.oauth.utils.LoginParam;
import com.noahplat.oauth.utils.RefreshDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotEmpty;

/**
 * 作者：李景星
 * 描述：用户登录controller
 * 时间：2020-11-02
 */
@RequestMapping("/api/oauth")
@RestController
@Validated
public class UserLoginController {

    @Value("${auth.clientId}")
    private String clientId;    //注入客户端ID
    @Value("${auth.clientSecret}")
    private String clientSecret;    //客户端密钥

    @Autowired
    private UserLoginService userLoginService;

    /**
     * 用户登录获取token
     * @param loginParam
     * @return
     */
    @PostMapping("/login")
    public Result<AuthToken> login(@RequestBody LoginParam loginParam){
        return userLoginService.login(loginParam, clientId, clientSecret);
    }

    /**
     * 用户刷新token令牌
     * @param refreshDTO
     * @return
     */
    @PostMapping("/refreshToken")
    public Result<AuthToken> refreshToken(@RequestBody RefreshDTO refreshDTO){
        return userLoginService.refreshToken(refreshDTO.getRefreshToken(), clientId, clientSecret);
    }
}
